﻿<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8" />
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" />
    <title>Demo演示-丰收联盟</title>
    <script type="text/javascript" src="https://wxmapp.zjcnbank.com//rootJs/jquery-3.3.1.min.js"></script>
    <script type="text/javascript" src="https://wxmapp.zjcnbank.com//rootJs/mfs_ios_compatible.js?v2"></script>
    <script type="text/javascript" src="https://wxmapp.zjcnbank.com//rootJs/mfs_common.js?v2"></script>
    <script type="text/javascript" src="https://wxmapp.zjcnbank.com//rootJs/mfs_oauth.js?v2"></script>
    <script>


        var port = 'http://fsyl.bjhnkj.cn/api/FS/Get_Base?action=';
        MFS.Oauth.clientId = '2019030100000000001001';
        MFS.Oauth.redirectUri = 'wxmapp.zjcnbank.com';
        MFS.Oauth.relevancyType = '2'; //紧密授权
        MFS.Oauth.scopeName = 'get_tight_user_info';
        MFS.Oauth.state = '3421432141234234';
        // 该接口为内部测试接口，
        MFS.Oauth.request_url = port + "GetClientSign";
        MFS.Oauth.user_info_url = port + 'GetUserInfoByToKen';
        MFS.Oauth.refesh_token_url = port + '/api/fslmUser/getClientSign/'
        ///实际操作时，通过openid获取用户，若不存在，需要启用授权，授权成功后，再次获取用户信息
        function getUserInfo() {
            oauth();
        }
        // 授权-start
        function oauth() {
            // 调出授权界面，获得授权后获得accessToken等信息
            MFS.Oauth.authorize({
                "callback": 'oauthCallback',
                "params": {
                    "clientId": MFS.Oauth.clientId,
                    "redirectUri": MFS.Oauth.redirectUri,
                    "state": MFS.Oauth.state,
                    "scopeList": [{
                        scopeName: MFS.Oauth.scopeName
                    }]
                }
            });
        }
        function oauthCallback(res) {
            var result = JSON.parse(res);
            if (result.rstCode == '1' && result.state == MFS.Oauth.state) {
                MFS.Oauth.accessToken = result.accessToken;
                MFS.Oauth.refreshToken = result.refreshToken;
                MFS.Oauth.getOpenId({
                    "callback": "getOpenIdCallback",
                    "params": {
                        "clientId": MFS.Oauth.clientId,
                        "accessToken": result.accessToken,
                        "refreshToken": result.refreshToken,
                        "scopeList": [{
                            "scopeName": MFS.Oauth.scopeName
                        }]
                    }
                });

            } else if (result.rstCode == '902') {
                MFS.webViewGoBack();
            }
        }
        function getOpenIdCallback(res) {
            var result = JSON.parse(res);
            if (result.rstCode == '1') {
                var openId = result.openId;
                var accessToken = MFS.Oauth.accessToken;
                var refreshToken = MFS.Oauth.refreshToken;
                if (accessToken && openId) {
                    backend_getUserInfo(openId, accessToken, refreshToken);
                }
            }
        }
        function backend_getUserInfo(openId, accessToken, refreshToken) {
            var data = {
                openId: openId,
                clientId: MFS.Oauth.clientId,
                relevancyType: MFS.Oauth.relevancyType
            };
            if (accessToken) {
                data.accessToken = accessToken;
                if (refreshToken) {
                    data.refreshToken = refreshToken;
                }
            }
            $.ajax({
                type: 'get',
                url: MFS.Oauth.user_info_url,
                data: data,
                dataType: 'json',
                beforeSend: function () {
                    document.getElementById('div_userinfo').innerHTML = "俺在狠努力得获取用户数据，你等等~";
                },
                success: function (data) {
                    if (data.code == 1) {
                        var json_result = data.data.model;
                        document.getElementById('div_userinfo').innerHTML = "用户手机号：" + json_result.user_name;
                    } else {
                        alert('前往柜面或者在线开通')
                        MFS.webViewGoBack()
                    }
                },
                error: function (err) {
                    document.getElementById('div_userinfo').innerHTML = "用户信息请求失败";
                    MFS.webViewGoBack()
                }
            });


        }
        //授权-end

        //刷新token start
        function backend_getRefreshToken(openId, refresh_token) {
            $.ajax({
                type: 'GET',
                url: 'http://fsyl.bjhnkj.cn/api/FS/Get_Base?action=GetClientSign',
                dataType: 'json',
                success: function (
                    data,
                    status
                ) {
                    if (data.code === 1) {
                        var client_sign = data.data.clientSign;
                        refreshAccessToken(
                            openId,
                            refresh_token,
                            client_sign
                        );
                    } else {
                        alert('client_sign错误: ' + data.message);
                        MFS.webViewGoBack()
                    }
                },
                error: function () {
                    alert('client_sign请求失败')
                    MFS.webViewGoBack()
                }
            });
        }
        //刷新token end

        //支付案例-start
        function getParam() {
            $.ajax({
                type: 'get',
                url: 'http://fsyl.bjhnkj.cn/api/FS/Get_Base?action=GetToPayInfo',
                datatype: "json",
                success: function (data) {
                    if (data.code === 1) {
                        var json_result = data.data.data;
                        $("#TransId").val(json_result.TransId);
                        $("#Plain").val(json_result.Plain);
                        $("#Signature").val(json_result.Signature);
                    }
                    else {
                        alert(data.msg);
                    }
                },
                error: function () {
                    alert('请求失败')
                }
            })
        }
        //支付案例-end

        //获取用户地址信息-start
        function getLocaTionAddress() {
            MFS.getLocation({
                callback: 'locationCallback'
            });
        }
        function locationCallback(res) {
            console.info('calback is: ' + res);
            var result = eval('(' + res + ")");
            getAddress(result.longitude, result.latitude);
        }
        function getAddress(lng, lat) {
            var point = BdmapEncryptToMapabc(lat, lng)
            var url = 'https://restapi.amap.com/v3/geocode/regeo?location=' + point.lng + ',' + point.lat + '&key=debdc460e60d6d22fdd60b90c2bfb365&radius=1000&extensions=all';
            $.ajax({
                type: 'get',
                url: url,
                datatype: "json",
                success: function (data) {
                    if (data.status == 1) { 
                        $("#address").html("GCJ-02坐标系:经度，纬度;国家;省份;城市;城市编码;坐标点所在区;行政区编码;坐标点所在乡镇/街道;乡镇街道编码;详细地址：" +
                            point.lng + "," +
                            point.lat + ";" +
                            data.regeocode.addressComponent.country + ";" +
                            data.regeocode.addressComponent.province + ";" +
                            data.regeocode.addressComponent.city + ";" +
                            data.regeocode.addressComponent.citycode + ";" +
                            data.regeocode.addressComponent.district + ";" +
                            data.regeocode.addressComponent.adcode + ";" +
                            data.regeocode.addressComponent.township + ";" +
                            data.regeocode.addressComponent.towncode + ";" +
                            data.regeocode.formatted_address + ":" + data.regeocode.addressComponent.streetNumber.location + "距离定位点" + data.regeocode.addressComponent.streetNumber.distance + "米")
                    }
                    else {
                        alert(data.msg);
                    }
                },
                error: function () {
                    alert('请求失败')
                }
            })
        }
        function BdmapEncryptToMapabc(bd_lat, bd_lon) {
            var point = new Object();
            var x_pi = 3.14159265358979324 * 3000.0 / 180.0;
            var x = new Number(bd_lon - 0.0065);
            var y = new Number(bd_lat - 0.006);
            var z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi);
            var theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi);
            var Mars_lon = z * Math.cos(theta);
            var Mars_lat = z * Math.sin(theta);
            point.lng = Mars_lon;
            point.lat = Mars_lat;
            return point;
        }
        //获取用户地址信息-end
    </script>
</head>

<body>
    <div style="color:red;font-weight:600">当前为demo数据，需按照实际功能进行自定义操作！</div>
    <div id="div_userinfo">
    </div>
    <button onClick="getUserInfo()">获取丰收银联用户信息-授权模式</button>
    <br />
    <div id="address">省份获得的坐标需要转换成GCJ-02坐标系</div>
    <button onClick="getLocaTionAddress()">获取当前用户地址</button>
    <br />
    <form action="https://epay1.zj96596.com/paygate/main" method="post" id="payForm">
        <input type="text" id="TransId" name="TransId" value="a" />
        <input type="text" id="Plain" name="Plain" value="b" />
        <input type="text" id="Signature" name="Signature" value="c" />
        <button type="button" id="getParamlal" onclick="getParam();">获取支付参数</button>
        <button type="submit" id="toPay">去支付</button>
    </form>
</body>


</html>